package com.thinkgem.dingdan.test;

import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFPrintSetup;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class PrintExcel {

	public static void main(String[] args) throws IOException {
        //新建文件输出流对象
        FileOutputStream out = new FileOutputStream("G:\\dev_desk\\文档\\自己的记录铺\\poitest.xls");
        //新建workbook
        HSSFWorkbook wb = new HSSFWorkbook();
        //新建sheet
        HSSFSheet sheet = wb.createSheet();
        //新建行
        HSSFRow row = sheet.createRow(2);
        //设置行高
        row.setHeightInPoints(20);
        //新建单元格
        HSSFCell cell = row.createCell(2);
        
        //创建整个文本的字体对象,workbook创建
        HSSFFont cnFont = wb.createFont();
        //设置字体行高，字体名字
        cnFont.setFontHeightInPoints((short)10);
        cnFont.setFontName("隶书");
        
        //将文本字面格式用到单元格上,新建单元格风格
        HSSFCellStyle cnStyle = wb.createCellStyle();
        cnStyle.setFont(cnFont);
        cell.setCellStyle(cnStyle);
        
        //单元格内文本对象新建，HSSFRichTextString的应用
        HSSFRichTextString richText = new HSSFRichTextString("中文字体测试");
        cell.setCellValue(richText);
        
        
        //再建一个单元格,重复上面的设置
        HSSFCell enCell = row.createCell(3);
        HSSFFont enFont = wb.createFont();    
        enFont.setFontHeightInPoints((short) 10);    
        enFont.setFontName("Arial Black");    
        HSSFCellStyle enStyle = wb.createCellStyle();    
        enStyle.setFont(enFont);    
        enCell.setCellStyle(enStyle);    
        enCell.setCellValue(new HSSFRichTextString("English font test"));    
        sheet.setColumnWidth(2, 4000);    
        sheet.setColumnWidth(3, 4000); 
        
        //输出
        //设置边框
        sheet.setDisplayGridlines(true);
        //设置打印的边框
        sheet.setPrintGridlines(true);
        
        //设置打印对象
        HSSFPrintSetup printSetup = sheet.getPrintSetup();
        //设置页边距
        printSetup.setHeaderMargin((double) 0.44); // 页眉
        printSetup.setFooterMargin((double) 0.2);//页脚
        
        //设置页宽
        printSetup.setFitWidth((short)1); 
        printSetup.setFitHeight((short)1000);
        
        //设置打印方向，横向就是true
        printSetup.setLandscape(true);
        //设置A4纸
        printSetup.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE);
        
        //打印，关闭流
        wb.write(out);
        out.close();
        
        System.out.println("ok");
    }
	
}
